import { navdatas } from '../data/navdatas'
import { createLeftNav } from './element'

// nav的切换效果
let iframe = document.getElementById('iframe1')
export function navReflect(els) {
    Array.prototype.forEach.call(els.children, (item, index) => {
        item.onclick = function () {
            Array.prototype.forEach.call(els.children, el => {
                el.classList.remove('item-active')
            })
            if (item.getAttribute('key') == index) {
                item.classList.add('item-active')
                // iframe跳转页面
                if (item.getAttribute('to'))
                    iframe.src = item.getAttribute('to')
                if (item.getAttribute('page')) {
                    changeLeftNav(navdatas, item.getAttribute('page'))
                }
            }
        }
    })
}
// 初始化导航栏事件
export function initNavEvent() {
    let uls = document.getElementsByClassName('nav-ul')
    navReflect(uls[0])
    navReflect(uls[1])
}


// 切换左导航栏数据
function changeLeftNav(datas, page) {
    let uls = document.getElementsByClassName('nav-ul')
    let leftNavDom = document.getElementById('leftNav')
    let navs = createLeftNav(datas, page)
    leftNavDom.innerHTML = ''
    leftNavDom.appendChild(navs)
    navReflect(uls[1])
    iframe.src = datas[page].children[0].to
}
